package cn.lwj.service.impl;

import cn.lwj.common.BaseContext;
import cn.lwj.mapper.AddressBookMapper;
import cn.lwj.pojo.AddressBook;
import cn.lwj.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {



    @Transactional
    @Override
    public void updateDefaultAdress(Long id) {
        //        将原来的默认值设为0
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBookByDefault = this.getOne(lqw); // 查询默认值为1的地址 如果存在则修改为0
        if(addressBookByDefault!=null){
            addressBookByDefault.setIsDefault(0);
            this.updateById(addressBookByDefault);
        }
//        修改收货地址的状态 （1为默认收货地址）
        if(id!=null){
            AddressBook addressBook = this.getById(id);
            addressBook.setIsDefault(1);
            this.updateById(addressBook);
        }
    }
}
